package il.ac.tau.cvws.scrabblesidekick.client.app.controller;

import il.ac.tau.cvws.scrabblesidekick.client.engine.GameEngine;
import il.ac.tau.cvws.scrabblesidekick.client.ui.GameActivity;
import il.ac.tau.cvws.scrabblesidekick.client.ui.ScrabbleSidekickActivity;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.ImageData;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.WordPlacement;

/* This class is intended as a controller/bridge between the upper platform's
 * UI controller (GameActivity) and the lower engine's logic (GameEngine).
 */
public class GameController extends CameraController
{
	private WordPlacement _currentWordPlacement;
	
	public GameController(ScrabbleSidekickActivity activity)
	{
		super(activity);
	}
	
	@Override
	protected GameEngine createEngine()
	{
		return new GameEngine(this);
	}
	
	public boolean start()
	{
		return ((GameEngine)_engine).startGame();
	}
	
	/* Once the user's letter rack has been entered, move on to the next step in
	 * the game flow -- display the camera and let the user snap a picture of the
	 * game board.
	 */
	public void handleRack(char[] rack)
	{
		((GameEngine)_engine).handleRack(rack);
		((GameActivity)_activity).showCameraLayout();
	}
	
	/* The game board's image was snapped -- move the image data (dimensions and
	 * actual data) downwards to the engine, and attempt to find a solution.
	 */
	@Override
	public void internalHandleHighQualityImage(int screenWidth, int screenHeight, ImageData image)
	{
		GameEngine engine = (GameEngine)_engine;
		
		engine.handleScreen(screenWidth, screenHeight);
		engine.handleBoardImage(image);
		
		engine.solveGame();
	}

	/* A solution was found. Store the solution here in the controller and notify
	 * the UI Activity of the success.
	 */
	public void handleGameSuccess(WordPlacement placement)
	{
		_currentWordPlacement = placement;
		
		((GameActivity)_activity).handleSuccess();
	}
	
	/* Get the solution which was found by the engine.
	 */
	public WordPlacement retrieveSoluion()
	{
		return _currentWordPlacement;
	}
}
